package com.xiaohua.tlgorders.service.impl;


import com.xiaohua.tlgorders.entity.Order;
import com.xiaohua.tlgorders.entity.OrderGoods;
import com.xiaohua.tlgorders.mapper.OrderGoodsMapper;
import com.xiaohua.tlgorders.mapper.OrderMapper;

import com.xiaohua.tlgorders.service.OrderService;

import com.xiaohua.tlgorders.tools.Record;
import com.xiaohua.tlgorders.tools.Status;
import com.xiaohua.tlgorders.util.ThreadPoolUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *@Author 小花
 *@Date 2019/7/17 11:50
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper mapper;
    @Autowired
    OrderGoodsMapper goodsMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Record modOrderSatus(Order order) {
        Record record =new Record();
        Integer i=mapper.modOrderStatus(order);
         if(i>0){
             record.setData(i);
             record.setMsg("更改订单状态");
             record.setStatus(Status.UPDATE_SUCCESS);
         }else{
             record.setMsg("更改失败");
             record.setStatus(Status.UPDATE_FALIED);
         }
        return record;
    }

    @Transactional
    @Override
    public Record modDataFlag(Order order) {
        Record record =new Record();
        Integer i=mapper.modDataFlag(order);
        if(i>0){
//            String key = "UserOrderId_" + order.getUserId();
//            if (redisTemplate.hasKey(key)) {
                //删除对应缓存
//                redisTemplate.delete(key);
                record.setData(i);
                record.setMsg("删除订单");
                record.setStatus(Status.REMOVE_SUCCESS);
//            }



        }else{
            record.setMsg("删除失败");
            record.setStatus(Status.REMOVE_FALIED);
        }
        return record;
    }
    @Transactional
    @Override
    public Record modIsClosed(Order order) {
        Record record =new Record();
        Integer i=mapper.modIsClosed(order);
        if(i>0){
            record.setData(i);
            record.setMsg("更改订单状态");
            record.setStatus(Status.UPDATE_SUCCESS);
        }else{
            record.setMsg("更改失败");
            record.setStatus(Status.UPDATE_FALIED);
        }
        return record;

    }
    @Transactional
    @Override
    public Record addOrders(Order order, OrderGoods goods) {


        Record record = new Record();

        try {
            System.out.println("------！！！！--------");
            Integer flag=mapper.createOrder(order);//新增订单
            Integer com= goodsMapper.createOrderGoods(goods);//新增订单

            if(flag>0&&com>0){
                    record.setMsg("新增顶单");
                    record.setData(flag+"|"+com);
                    record.setStatus(Status.INSERT_SUCCESS);
            }else{
                record.setMsg("新增失败");
                record.setData(flag+"|"+com);
                record.setStatus(Status.INSERT_FALIED);
            }




    } catch (Exception e) {
            record.setMsg("新增异常");
            record.setData(e);
            record.setStatus(Status.INSERT_FALIED);
        } finally {
            return record;
        }


    }
    @Transactional
    @Override
    public String findRepeatOrder(String orderNo) {
        return mapper.findRepeatOrder(orderNo);
    }
    @Transactional
    @Override
    public void modIsPay(String orderNo) {
         mapper.modIsPay(orderNo);
    }
    @Transactional
    @Override
    public List<String> findTimeValue() {
        return mapper.findTimeValue();
    }
    @Transactional
    @Override
    public void batchMod(String orderNo) {
        mapper.batchMod(orderNo);
    }
    @Transactional
    @Override
    public Record orderCount(String orderNo, Integer orderStatus,Integer userId) {
        Integer i=mapper.orderCount(orderNo,orderStatus,userId);
        Record record = new Record();
        if(i>0) {

            record.setStatus(Status.SELECT_SUCCESS);
            record.setData(i);
            record.setMsg("查询数量成功");
        }else {

            record.setStatus(Status.SELECT_FALIED);
            record.setData(null);
            record.setMsg("查询失败");
        }
        return record;
    }

    @Transactional
    @Override
    public Record ModIsRefund(Integer isRefund, String orderNo) {


        Record record=new Record();
        try {
            record.setMsg("修改退款状态");
            record.setData(mapper.ModIsRefund(isRefund,orderNo));
            record.setStatus(Status.UPDATE_SUCCESS);
        }catch (Exception e){
            record.setMsg("修改出错");
            record.setData(e);
            record.setStatus(Status.UPDATE_FALIED);
        }finally {
            return record;
        }
    }
    @Transactional
    @Override
    public Record ModReason(String orderNo, String rejectOtherReason) {
        Record record=new Record();
        try {
            record.setMsg("新增退款原因");
            record.setData(mapper.ModReason(orderNo,rejectOtherReason));
            record.setStatus(Status.UPDATE_SUCCESS);
        }catch (Exception e){
            record.setMsg("修改出错");
            record.setData(e);
            record.setStatus(Status.UPDATE_FALIED);
        }finally {
            return record;
        }
    }

}
